/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.eclipse.jetty.server.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.SignupConfig;
import com.boxin.ims.modules.wechat.service.SignupConfigService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 连续签到配置
 * @author xiaoqian
 *
*	2014上午9:46:15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/signupConfig")
public class SignupConfigController extends BaseController {

	@Autowired
	private SignupConfigService signupConfigService;
	
	@ModelAttribute
	public SignupConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return signupConfigService.get(id);
		}else{
			return new SignupConfig();
		}
	}
	
	@RequiresPermissions("wechat:signupConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(SignupConfig signupConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		
		if (!user.isAdmin()){
			signupConfig.setUser(user);
		}
        Page<SignupConfig> page = signupConfigService.find(new Page<SignupConfig>(request, response), signupConfig); 
        model.addAttribute("page", page);
		return "modules/wechat/signupConfigList";
	}

	@RequiresPermissions("wechat:signupConfig:view")
	@RequestMapping(value = "form")
	public String form(SignupConfig signupConfig, Model model) {
		User user = UserUtils.getUser();
		SignupConfig temp = signupConfigService.getByUserId(user.getId());
		if(temp!=null){
			signupConfig = temp;
		}else{
			signupConfig.setUser(user);
		}
		
		
		model.addAttribute("signupConfig", signupConfig);
		return "modules/wechat/signupConfigForm";
	}

	@RequiresPermissions("wechat:signupConfig:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,SignupConfig signupConfig, Model model, RedirectAttributes redirectAttributes) {
		
		User user = UserUtils.getUser();
		if (!beanValidator(model, signupConfig)){
			
			return form(signupConfig, model);
		}
		signupConfig.setScore((int)(Double.parseDouble(request.getParameter("pScore"))*1000));
		signupConfig.setMaxScore((int)(Double.parseDouble(request.getParameter("pMaxScore"))*1000));
		signupConfigService.save(signupConfig);
		if(user.isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/signupConfig/?repage";
		}else{
			return form(signupConfig, model);
		}
	}
	
	@RequiresPermissions("wechat:signupConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		signupConfigService.delete(id);
		User user = UserUtils.getUser();
		addMessage(redirectAttributes, "删除抽奖配置成功");
		if(user.isAdmin()){
			return "redirect:"+Global.ADMIN_PATH+"/wechat/signupConfig/?repage";
		}else{
			return  "modules/wechat/signupConfigForm";
		}
	}

}
